package com.smm.web.vmc.api;

import com.smm.web.vmc.domain.*;
import com.smm.web.vmc.dto.JsonReturn;
import com.smm.web.vmc.service.CameraService;
import com.smm.web.vmc.service.ReceiptService;
import com.smm.web.vmc.service.WarehouseService;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 仓库api
 */
@Controller
@RequestMapping(value = "/api/warehouse",method = RequestMethod.POST)
public class WarehoseApi {


    @Resource
    private WarehouseService warehouseService;

    @Resource
    private ReceiptService receiptService;

    @Resource
    private CameraService cameraService;


    //按仓库名称搜索
    @ResponseBody
    @RequestMapping("searchByName.do")
    public JsonReturn searchByName(String name){

        try{

            Pager pager = new Pager(1);

            pager.setRowCount(1);

            List<Warehouse>  warehouses = this.warehouseService.searchByName(name);

            Map<String ,Object> result = new HashMap<>();

            result.put("list",warehouses);

            result.put("pager",pager);

            return new JsonReturn(1,"success",result);

        }catch (Exception e){

            e.printStackTrace();

            return new JsonReturn(0,e.getMessage());

        }

    }


    @RequestMapping("/listByArea.do")
    @ResponseBody
    public JsonReturn listByArea(int pageNo,String area){

        Pager pager = new Pager(pageNo);

        List<Warehouse>  warehouses = this.warehouseService.listByAreaAndPage(pager,area);

        Map<String ,Object> result = new HashMap<>();

        result.put("list",warehouses);

        result.put("pager",pager);

        return new JsonReturn(1,"success",result);

    }


    @RequestMapping("/listByAreaAndType.do")
    @ResponseBody
    public JsonReturn listByAreaAndType(int type,int pageNo,String area){

        Pager pager = new Pager(pageNo);

        List<Warehouse>  warehouses = this.warehouseService.listByAreaAndTypeAndPage(pager,area,type);

        Map<String ,Object> result = new HashMap<>();

        result.put("list",warehouses);

        result.put("pager",pager);

        return new JsonReturn(1,"success",result);

    }



    @RequestMapping("/list.do")
    @ResponseBody
    public JsonReturn list(int pageNo){

        Pager pager = new Pager(pageNo);

        List<Warehouse>  warehouses = this.warehouseService.listForPage(pager);

        Map<String ,Object> result = new HashMap<>();

        result.put("list",warehouses);

        result.put("pager",pager);

        return new JsonReturn(1,"success",result);

    }


    @RequestMapping("/listByType.do")
    @ResponseBody
    public JsonReturn listByType(int type,int pageNo){

        Pager pager = new Pager(pageNo);

        List<Warehouse>  warehouses = this.warehouseService.listByTypeForPage(type,pager);

        Map<String ,Object> result = new HashMap<>();

        result.put("list",warehouses);

        result.put("pager",pager);

        return new JsonReturn(1,"success",result);

    }



    @RequestMapping("/all.do")
    @ResponseBody
    public JsonReturn all(){

        List<Warehouse>  warehouses = this.warehouseService.getAll();

        return new JsonReturn(1,"success",warehouses);

    }


    @RequestMapping("/info.do")
    @ResponseBody
    public JsonReturn info(int id){

        Warehouse warehouse = this.warehouseService.getOneById(id);

        //获取仓库图片
        List<WarehouseImage> imageList = this.warehouseService.getImageListByid(id);

        //获取仓单列表
        List<Receipt> receipts = this.receiptService.getAllByWarehouser(id);


        //获取摄像头列表
        List<Camera> cameras = this.cameraService.getAllByWarehouse(id);

        Map<String ,Object> result = new HashMap<>();

        result.put("warehouse",warehouse);
        result.put("imageList",imageList);

        result.put("receipts",receipts);
        result.put("cameras",cameras);

        return new JsonReturn(1,"success",result);

    }




}
